package com.peihua.controller;

import com.peihua.domain.Result;
import com.peihua.domain.vo.InvoiceListItem;
import com.peihua.dto.SaveInvoiceRequest;
import com.peihua.service.InvoiceTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/invoice")
public class InvoiceController {
    @Autowired
    private InvoiceTitleService invoiceTitleService;
    /**
     * 列出发票抬头（供发票助手页渲染）
     */
    @GetMapping("/list")
    public Result list() {
        List<InvoiceListItem> items = invoiceTitleService.listAll();
        return Result.success(items);
    }
    /**
     * 保存企业抬头
     */
    @PostMapping("/save")
    public Result save(@RequestBody SaveInvoiceRequest request) {
        if (request == null) {
            return Result.error(400L, "请求体不能为空");
        }
        if (!"企业".equals(request.getTitleType())) {
            return Result.error(400L, "仅支持企业抬头保存");
        }
        boolean ok = invoiceTitleService.saveEnterprise(request);
        if (ok) {
            return Result.success();
        }
        return Result.error(500L, "保存失败，请重试");
    }
}